﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DeVetma.admin.classes;
using DeVetma.admin.models.database.metadata;
using DeVetma.admin.models.managers;

namespace DeVetma.admin.models.entities
{
    [Entity(table = "Order_Details")]
    public class OrderDetails
    {
        [EntityField(column="odID", keyField = true)]
        public string odID { get; set; }

        [EntityField(column = "oID", keyField = false)]
        public string oID { get; set; }

        [EntityField(column = "cID", keyField = false)]
        public string cID { get; set; }

        [EntityField(column = "odPrice", keyField = false, nullValue = 0.0)]
        public decimal odPrice { get; set; }

        private int _odQuantity;
        private int _odQuantityOld;

        [EntityField(column = "odQuantity", keyField = false)]
        public int odQuantity 
        {
            get { return _odQuantity; }
            set
            {
                _odQuantityOld = _odQuantity;
                _odQuantity = value;
            }
        }

        public Order order
        {
            get { return getOrder(); }
            set { this.oID = value.oID; }
        }

        public Clothes clothe
        {
            get { return getClothe(); }
            set { this.cID = value.cID; }
        }

        public decimal subtotal
        {
            get { return getSubtotal(); }
        }

        public OrderDetails()
        {
            odQuantity = 0;
        }

        private Order getOrder()
        {
            return new OrderManager().get(this.oID);
        }

        private Clothes getClothe()
        {
            return new ClothesManager().get(this.cID);
        }

        private decimal getSubtotal()
        {
            return this.odQuantity * this.odPrice;
        }

        public void commitQtyUpdate()
        {
            _odQuantityOld = _odQuantity;
        }

        public int getODQuantityOld()
        {
            return _odQuantityOld;
        }

        public override bool Equals(object obj)
        {
            if (obj is OrderDetails && odID == ((OrderDetails)obj).odID)
                return true;

            return false;
        }
    }
}